//#region 退出功能
const logoutA = document.querySelector('.logout a')
logoutA.addEventListener('click', function () {
  if (confirm('r u sure?')) {
    localStorage.removeItem('token')
    location.href = './login.html'
  }
})
//#endregion

//#region 侧边导航 一级标题

const topList = document.querySelectorAll('.nav>li>a')
topList.forEach(dom => {
  dom.addEventListener('click', function () {
    dom.nextElementSibling.classList.toggle('show')
  })
})
//#endregion

//#region 二级标题
const subList = document.querySelectorAll('.nav li ul a')
subList.forEach(dom => {
  dom.addEventListener('click', function () {
    document.querySelector('.nav li ul a.active').classList.toggle('active')
    this.classList.add('active')
  })
})

//#endregion

//#region 初始化数据
const initBtn = document.querySelector('.init')
initBtn.addEventListener('click', function () {
  axios
    .get('/init/data', {
      headers: {
        Authorization: localStorage.getItem('token')
      }
    })
    .then(result => {
      if (result.data.code === 0) {
        toastr.error(result.data.message)
      } else {
        toastr.success(result.data.message)
      }
    })
})
//#endregion